var global; 
var settings;
var numPages;  

function sort(cols){

for(var i=0;i<global.length;i++)
{
  
   var obj = global[i]; 
   
   var newJson = {}; 
   
   for(var at in cols){
   
    newJson[at] = obj[at]; 
   
   }
   
   global[i] = newJson; 

}

}

function calculate()
{

	   pagination = new Array(); 
	  
	   numPages = parseInt(global.length/settings.pageSize); 
	  
	  numPages = ((global.length%settings.pageSize)>0 ? (numPages+1):numPages); 
	  var count = 0; 
	  
	  for(var i=0;i<numPages;i++)
	  {
	  
	     var line = new Array(settings.pageSize); 
		 for(j=0;j<line.length;j++)
		 {
		     if(count>=global.length)
			  break ; 
		 
		 line[j] = global[count]; 
		 count++; 
		 
		 }
		  
		  pagination[i] = line; 
	  
	  }



}

function equals(objeto1, objeto2)
{

var resultado = true; 

for( var attr in objeto1)
{

 if(objeto1[attr]!=objeto2[attr])
  {
    resultado = false;
	break;
  
  }
  

}

return resultado; 

}

function createLines(objetos,target){
	
	$(target).children('tbody').html(''); 
	
		  for(var i=0;i<objetos.length;i++) 
	  {
	    
		$(target).children('tbody').append('<tr></tr>'); 
		
		var ob = objetos[i]; 
	
		for(atributo in ob)
		{
		   $(target).children('tbody').children('tr:last').append("<td title='"+ob[atributo]+"'>"+ob[atributo]+'</td>'); 
		   // adiciona listener de click 
		   $(target).children('tbody').children('tr:last').click(function(){
		   
		     $('tr.selected').removeClass('selected'); 
		     $(this).addClass('selected'); 
		   
		   }); 
		   
		}
	  
	  }
	  
	  // cria efeito zebrado 
	  $(table).children('tbody').children('tr:odd').addClass('odd');
      $(table).children('tbody').children('tr:even').addClass('even');
      
      if(settings.listener!=null)
    	  settings.listener(); 
	
	}
	
	
	
	
	
 function getSelected(tabela){

json = {}; 
	
	 $(tabela).children('table').children('tbody').children('tr.selected').children('td').each(function(i){
	
	templete = global[0]; 
	var count =0; 
	for(valor in templete)
	{
	  if(i==count)
	  {
	   
	   json[valor] = $(this).html()
	   break; 
	  }
	  
	  count++; 
	  
	}
	
	
	
	}); 
	
	return json; 
	

}

jQuery.fn.jtable_remove = function remove(){


return this.each(function(){


var index; 

$('table tbody tr').each(function(k){

if($(this).hasClass('selected'))
index = k; 

}); 


  global = $.grep(global,function(n,l){
  
  var pos = (currentPage*settings.pageSize)+index;

    return l!=pos; 
     
  });
// verificar aqui caso de erro
  
  if(global.length==0)
	 {
	    createLines([],table);
	    currentPage = 0;
	    return; 
	 } 
	  
     calculate(); 
  
if(pagination.length==currentPage && currentPage!=0)
{

$('.previous').click(); 

}
else 
createLines(pagination[currentPage],table);   


}); 


}

jQuery.fn.jtable =  function tabela(colunas,objetos,set){

global = objetos; 

sort(colunas); 

 if(tabela.arguments.length<4 && settings==null)
    settings = {pageSize:4,align:'left'}; 
	
settings = set; 
	

    return this.each(function(){

	
	$(this).html(''); 
	
	
      $('<table></table>').appendTo(this);
      table = $(this).children('table'); 	  
	  $('<thead></thead>').appendTo(table); 
	  $(table).children('thead').append('<tr></tr>');
	  
	  // cria columns 
	  for(at in colunas)
	  $(table).children('thead').children('tr').append('<th align='+settings.align+'>'+colunas[at]+'</th>')
	  
	  // cria corpo da tabela
	  $(table).append('<tbody></tbody>'); 
	  
	  if(global.length==0)
	    return; 
	  // cria vetor de paginacao 
      calculate(); 
	  
	  // cria lines 
	  currentPage = 0; 
      createLines(pagination[0],table); 
	  	  
	  // cria menu de paginacao
	  
	  $(this).append('<ul></ul>'); 
	  $(this).children('ul').addClass('pagination'); 
	  $(this).children('ul').append("<li><a href='#'>Anterior</a></li><li><a href='#'>Pr&oacute;ximo</a></li>"); 
	  $(this).children('ul').children('li:first').addClass('previous'); 
	  $(this).children('ul').children('li:last').addClass('next');
	  
	  $('.next').click(function(){
	     if(currentPage<(pagination.length-1))
		 {
		   currentPage++; 
		   
		   $(table).children('tbody').fadeOut(500,function(){
		
		   createLines(pagination[currentPage],table); 
		   $(this).fadeIn(); 
		
		
		})
		   
		   
		 
		 }
		 
	 
	  
	  }); 
	  
	  $('.previous').click(function(){
	  
	  if(currentPage>0)
	  {
	    currentPage--; 
		$(table).children('tbody').fadeOut(500,function(){
		
		createLines(pagination[currentPage],table); 
		$(this).fadeIn(); 
		
		
		})
		
	  
	  }
	  
	  
	  }); 
	  

        
	  
    });
	
	
};
